const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin'); //css单独打包
const extend = require('extend');

//判断当前运行环境是开发模式还是生产模式
const nodeEnv = process.env.NODE_ENV || 'development';

//svg
const svgSpriteDirs = [
    // require.resolve('antd-mobile').replace(/warn\.js$/, ''), // antd-mobile 内置svg
    path.resolve(__dirname, 'src/my-project-svg-foler'),  // 业务代码本地私有 svg 存放目录
];

const config = {

    entry: {
        libs: [],
        app: './main.js'
    },

    output: { //入口文件输出配置
        path: __dirname + '/build/',
        publicPath: '/',
        filename: '[name].js',
        chunkFilename: '[name].bundle.js',
    },

    module: {
        rules: [
            {
                test: /\.json$/,
                use: 'json-loader'
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: 'babel-loader'
            },
            {
                test: /\.css$/,
                exclude: /^node_modules$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            {
                test: /\.less$/,
                exclude: /^node_modules$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ['css-loader', 'less-loader']
                })
            },
            {
                test:  /\.(png|woff|woff2|svg|ttf|eot|jpg|gif)$/,
                use: "url-loader?limit=10000" //限制大小小于10k的
            },
            {
                test: /\.svg$/,
                use: 'svg-sprite-loader',
                include: svgSpriteDirs
            }
        ]
    },

    //resolve,解决 antd-mobile 代码查找问题
    resolve: {      
        mainFiles: ["index.web","index"],// 这里哦
        modules: [path.resolve(__dirname, "src"), "node_modules"],
        extensions: ['.web.tsx', '.web.ts', '.web.jsx', '.web.js', '.ts', '.tsx', '.js', '.jsx', '.json'],
    },


    plugins: [ //将index.tmpl.html转化为最终的index.html，会自动引入bundle.js
        new ExtractTextPlugin("[name].css"),
        new webpack.DefinePlugin({
            'NODE_ENV': JSON.stringify(nodeEnv)
        }),
        new HtmlWebpackPlugin({
            template: __dirname + '/src/template/index.tmpl.html',
            inject: true,
            hash: true
        })
    ]

}

module.exports = config;